<?php

/**
 * @package    [PACKAGE_NAME]
 *
 * @author     [AUTHOR] <[AUTHOR_EMAIL]>
 * @copyright  [COPYRIGHT]
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @link       [AUTHOR_URL]
 */

require_once(JPATH_ROOT . '/components/com_jcomments/jcomments.class.php');

use Joomla\CMS\MVC\Controller\BaseController;

use Joomla\CMS\Log\Log;


defined('_JEXEC') or die;

/**
 * WxMinipro controller.
 *
 * @package  [PACKAGE_NAME]
 * @since    1.0.0
 */
class WxMiniproControllerWxItem extends BaseController
{

    public function display($cachable = false, $urlparams = array())
    {
        echo "hell wxitem";
        jexit();
        return;
    }

    public function  addcomment()
    {
        $res = [
            'result' =>   -1,
            'errmsg' => '添加评论 未知错误',
        ];

        $jinput = $this->input;;
        $skey = $jinput->get('skey');
        if (!$skey) {
            $res['errmsg'] = '添加评论--缺少 session key';
            echo json_encode($res);
            jexit();
        }

        $userInforaw = $jinput->get('userInfo', null, 'RAW');
        if ($userInforaw) {
            $userinfo = json_decode($userInforaw);
        } else {
            $res['errmsg'] = '添加评论--用户没有登录';
            echo json_encode($res);
            jexit();
        }

        $content = $jinput->get('content', null, 'RAW');

        // $res['errmsg'] = $content;
        // echo json_encode($res);
        // jexit();

        $itemid = $jinput->get('itemid');
        $catid = $jinput->get('catid');
        $link = $jinput->get('link');
        $title = $jinput->get('title');

        // 更新jcomments表,添加评论
        JTable::addIncludePath(JPATH_ROOT . '/administrator/components/com_jcomments/tables');
        $comment = JTable::getInstance('Comment', 'JCommentsTable');
        $comment->object_id = $itemid;
        $comment->object_group = 'com_k2';
        $comment->lang = 'zh-CN';
        $comment->username = $skey;
        $comment->name = $userinfo->nickName;
        $comment->homepage = $userinfo->avatarUrl;
        $comment->comment = $content;
        $comment->published = 1;
        $comment->date = date("Y-m-d H:i:s");
        $result_com = $comment->store();

        // Get a db connection.
        $db = JFactory::getDbo();

        // 更新jcomments_objects表
        JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_wxminipro/tables');
        $objtbl = JTable::getInstance('WxCommentObject', 'Table', array());
        $result_obj = $objtbl->getIdby($itemid);
        if (!$result_obj) {
            $object = new stdClass();
            $object->object_id = $itemid;
            $object->object_group = 'com_k2';
            $object->category_id = $catid;
            $object->lang = 'zh-CN';
            $object->title = $title;
            $object->link = $link;
            $object->access = 1;
            $object->userid = 153;
            $object->expired = 0;
            $object->modified = date("Y-m-d H:i:s");
            $result_obj = $db->insertObject('#__jcomments_objects', $object);
        }
        if ($result_com && $result_obj) {
            $res['errmsg'] = '添加评论成功';
            $res['result'] = 0;
        }
        echo json_encode($res);
        jexit();
    }
}
